var Global = require("../util/global.js");
var websocket = require("../util/websocket.js");
var message = require("../util/message.js");

cc.Class({
    extends: cc.Component,

    properties: {
       iName:cc.Label,
       uName:cc.Label,
    },

    // onLoad () {},

    start () {
        this.init();
    },
    init(){
        this.initWebSocket();

        this.initName();
    },
    initWebSocket(){
        if(Global.socket){
            Global.socket.sock.parent = this;
            Global.socket.sock.onmessage = this.onMessage;
        }
    },
    initName(){
        let i,u;
        for(let x=0;x<Global.room.userList.length;x++){
            if(Global.room.userList[x].id==Global.userId){
                i = Global.room.userList[x];
            }else{
                u = Global.room.userList[x];
            }
        }

        this.iName.node.getComponent(cc.Label).string = i.name;
        this.uName.node.getComponent(cc.Label).string = u.name;
    },
    onReqStartFightClick(){
        this.requestFight();
    },
    requestFight(){
        message.type = 100070;
        message.content = {};
        Global.socket.sendData(message);
    },
    onMessage(event){
        cc.log("接收消息:" + event.data);

        var self = this.parent;

        var message = JSON.parse(event.data);

        if(message.type==200070){
            cc.director.preloadScene("game", function () {
                cc.director.loadScene('game');
            });
        }
    },

    // update (dt) {},
});
